//引入express模块
const express = require("express");
//创建路由器对象
const router = express.Router();
//引入连接池模块
const pool = require("../pool.js");
module.exports = router;

//查看全部地址的接口
router.post("/address", (req, res) => {
  //获取地址栏中的uname参数
  let uname = req.query.uname;
  // SQL查询
  let sql =
    "SELECT id,user_id,name,isDefault,tel,province,city,county,addressDetail,concat(province,city,county,addressDetail) AS address FROM `xz_receiver_address` WHERE user_id=? ORDER BY `xz_receiver_address`.`isDefault`  DESC";
  // 执行SQL查询
  pool.query(sql, [uname], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    res.send({ message: "ok", code: 200, results: results });
  });
});

//按序号删除地址的接口
router.post("/delAddress", (req, res) => {
  //获取地址栏中的uname参数
  let id = req.query.id;
  // SQL查询
  let sql = "DELETE FROM `xz_receiver_address`WHERE id =?";
  // 执行SQL查询
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    res.send({ message: "ok", code: 200, results: results[0] });
  });
});

//按序号查看地址的接口
router.post("/addressId", (req, res) => {
  //获取地址栏中的uname参数
  let id = req.query.id;
  // SQL查询
  let sql = "SELECT * FROM `xz_receiver_address`WHERE id =?";
  // 执行SQL查询
  pool.query(sql, [id], (error, results) => {
    if (error) throw error;
    // 返回数据到客户端
    res.send({ message: "ok", code: 200, results: results[0] });
  });
});

//查询地址id的接口
router.get("/getAddressId", (req, res) => {
  pool.query("select * from xz_receiver_address", (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);
    if (r.length == 0) {
      res.send({ code: 400, msg: "查询失败", data: r });
    } else {
      res.send({ code: 200, msg: "查询成功", data: r });
    }
  });
});

// 修改地址数据的接口
router.post("/updateAddress", (req, res) => {
  //获取地址栏中的id参数
  let id = parseInt(req.body.id);
  let name = req.body.name;
  let phone = req.body.phone;
  let province = req.body.province;
  let city = req.body.city;
  let county = req.body.county;
  let detail_addr = req.body.detail_addr;
  let group_message = req.body.group_message;
  let areaCode = req.body.areaCode;
  let postalCode = req.body.postalCode;
  // SQL查询
  let sql =
    "UPDATE xz_receiver_address SET name =?, province =?, city =?, county =?, addressDetail =?, tel =?, postalCode =?, areaCode =?, isDefault =? WHERE id =?";
  // 执行SQL查询
  pool.query(
    sql,
    [
      name,
      province,
      city,
      county,
      detail_addr,
      phone,
      postalCode,
      areaCode,
      group_message,
      id,
    ],
    (error, results) => {
      if (error) throw error;
      // 返回数据到客户端
      if (results.length == 0) {
        //登录失败
        res.send({ message: "login failed", code: 201 });
      } else {
        //登录成功
        res.send({ message: "ok", code: 200, result: results });
      }
    }
  );
});

// 添加地址数据的接口
router.post("/addAddress", (req, res) => {
  //获取地址栏中的id参数
  let uname = req.body.uname;
  let name = req.body.name;
  let phone = req.body.phone;
  let province = req.body.province;
  let city = req.body.city;
  let county = req.body.county;
  let detail_addr = req.body.detail_addr;
  let group_message = req.body.group_message;
  let areaCode = req.body.areaCode;
  let postalCode = req.body.postalCode;
  // SQL查询
  let sql =
    "INSERT `xz_receiver_address` (`user_id`, `name`, `province`, `city`, `county`, `addressDetail`, `tel`, `postalCode`, `areaCode`, `isDefault`) VALUES (?,?,?,?,?,?,?,?,?,?) ";
  // 执行SQL查询
  pool.query(
    sql,
    [
      uname,
      name,
      province,
      city,
      county,
      detail_addr,
      phone,
      postalCode,
      areaCode,
      group_message,
    ],
    (error, results) => {
      if (error) throw error;
      // 返回数据到客户端
      if (results.length == 0) {
        //请求失败
        res.send({ message: "get failed", code: 201 });
      } else {
        //请求成功
        res.send({ message: "ok", code: 200, result: results });
        console.log(phone);
      }
    }
  );
});
